package com.zmn.oms.dubbo.impl.zmn.normal.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.business.interfaces.work.OrderWorkPositionBService;
import com.zmn.oms.common.dro.position.OrderDistanceDRO;
import com.zmn.oms.dubbo.dto.zmn.order.OrderWorkPositionDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.order.OrderWorkPositionListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.work.OrderWorkPosition;
import com.zmn.oms.services.interfaces.work.OrderWorkPositionService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
 * 工单距离查询远程服务实现
 *
 * @author hankangshuai
 * @since 2020/08/28
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderWorkPositionListRemoteServiceImpl implements OrderWorkPositionListRemoteService {

    /**
     * 对象转换
     */
    public static final BeanCopier TO_DRO_COPIER = BeanCopier.create(OrderWorkPosition.class, OrderWorkPositionDRO.class, false);
    @Resource
    private OrderWorkPositionService orderWorkPositionService;
    @Resource
    private OrderWorkPositionBService orderWorkPositionBService;
    @Resource
    private OrderWorkService orderWorkService;

    @Override
    public ResponseDTO<OrderWorkPositionDRO> getByWorkId(@NotNull(message = "工单ID不可为null") Long workId) {
        OrderWorkPosition workPosition = orderWorkPositionService.findById(workId);
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(workId, workId);

        ResponseDTO<OrderWorkPositionDRO> responseDTO = Optional.ofNullable(workPosition)
                // 转换dro
                .map(orderWorkPosition -> {
                    OrderWorkPositionDRO dro = new OrderWorkPositionDRO();
                    Integer distance = orderWorkPositionBService.customerMinPayDistance(orderWork.getWorkId(), orderWork.getVisitUserId(),
                            workPosition.getMasterPayLongitude(), workPosition.getMasterPayLatitude(), workPosition);
                    orderWorkPosition.setMasterPayDistance(distance);
                    TO_DRO_COPIER.copy(orderWorkPosition, dro, null);
                    return dro;
                })
                // 封装为ResponseDTO
                .map(ResponseDTO::success)
                // 为null返回成功但数据为Null
                .orElseGet(ResponseDTO::success);
        log.info("查询工单[{}]支付距离：{}", workId, JSON.toJSONString(responseDTO));
        return responseDTO;
    }

    @Override
    public ResponseDTO<Integer> getOrderDistance(@NotNull(message = "入参不能为空") OrderDistanceDRO orderDistanceDRO) {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderDistanceDRO.getOrderId(), orderDistanceDRO.getWorkId());
        Integer distance = orderWorkPositionBService.customerMinPayDistance(orderDistanceDRO.getWorkId(), orderWork.getVisitUserId(), orderDistanceDRO.getLongitude()
                , orderDistanceDRO.getLatitude());
        return ResponseDTO.success(distance);
    }
}
